﻿Public Class NewEmp


    Public l As Integer = 0
    Public pos As Int16 = 0
    Public se As Int16 = 0
    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click

        cmd.Parameters("@no") = txtno.Text
        cmd.Parameters("@name") = txtname.Text
        cmd.Parameters("@dob") = dtpdob.Value
        cmd.Parameters("@pob") = txtpob.Text
        cmd.Parameters("@salary") = txtsalary.Text
        cmd.Parameters("@adr") = txtadr.Text
        cmd.Parameters("@phone") = txtphone.Text
        cmd.Parameters("@sex") = cbosex.SelectedValue
        cmd.Parameters("@pos") = cbopos.SelectedValue
        cmd.Parameters("@mail") = txtmail.Text
        cmd.Parameters("@ac") = 1

        If Me.PictureBox1.Image Is Nothing Then
            cmd.Parameters("@PHOTO") = DBNull.Value
        Else
            Dim m As New System.IO.MemoryStream()
            Me.PictureBox1.Image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim bytes As Byte() = m.GetBuffer()
            cmd.Parameters("@PHOTO") = bytes
        End If

        If l = 0 Then
            cmd.ExecuteNonQuery("INSERT INTO employee_tbl VALUES(@no,@name,@dob,@pob,@salary,@adr,@phone,@sex,@pos,@mail,@ac,@PHOTO)")
        Else
            cmd.Parameters("@id") = l
            cmd.ExecuteNonQuery("UPDATE employee_tbl SET emp_name=@name,emp_dob=@dob,emp_pob=@pob,emp_salary=@salary,emp_adr=@adr,emp_phone=@phone,sex_id=@sex,emp_pos_id=@pos,emp_mail=@mail,photo=@PHOTO WHERE emp_id=@id")
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnbrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrow.Click
        Dim diag As New OpenFileDialog()
        diag.Filter = "Image file |*.jpg|Bitmap Picture|*.bmp"
        If diag.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim fileName = diag.FileName
            Me.PictureBox1.Image = Image.FromFile(fileName)
        End If
    End Sub

    Private Sub NewEmp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If txtno.Text = "" Then
            loadno()
        Else
            txtno.Enabled = False
        End If

        loadpos()
        loadsex()
        If l > 0 Then
            cbopos.SelectedValue = pos
            cbosex.SelectedValue = se
        End If
    End Sub
    Private Sub loadsex()
        cbosex.DisplayMember = "Sex"
        cbosex.ValueMember = "Sex_Id"
        cbosex.DataSource = cmd.ExecuteDataTable("SElECT * FROM Sex_tbl")
    End Sub

    Private Sub loadpos()
        cbopos.DisplayMember = ("emp_pos_name")
        cbopos.ValueMember = "emp_pos_id"
        cbopos.DataSource = cmd.ExecuteDataTable("SELECT * FROM employee_position_tbl")
    End Sub


    Private Sub loadno()
        'Dim dt As New DataTable
        txtno.Enabled = False
        Dim b As String = cmd.ExecuteScalar("SELECT MAX(emp_no) FROM employee_tbl")
        Dim a As Int16 = b.Replace("EMP", "") + 1
        txtno.Text = a.ToString("EMP0000")
    End Sub
End Class